//-----------------------------------------------------------------------
// <copyright file="TestFiles.cs" company="Anthony Steele">
// This source forms part of the DuplicateFinder utility
// Hosted at http://www.codeplex.com/DuplicateFinder
//
// This source is available under the terms of the Microsoft Public License (Ms-PL)
// See http://duplicatefinder.codeplex.com/license
// All other rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace DuplicateFinder.TestLibrary
{
    using DuplicateFinderLib;

    using NUnit.Framework;

    /// <summary>
    /// Run tests on the test data files
    /// </summary>
    [TestFixture]
    public class TestFiles
    {
        #region tests

        /// <summary>
        /// Test creating an instance
        /// </summary>
        [Test]
        public void TestSimpleCreate()
        {
            DuplicateFinderEngine process = new DuplicateFinderEngine();
            Assert.IsNotNull(process);
        }

        /// <summary>
        /// use the test file TestDuplicate5.txt
        /// </summary>
        [Test]
        public void TestDuplicate5()
        {
            FileDuplicateFinderTester tester = new FileDuplicateFinderTester();
            FileDuplicateFinder process = tester.Process;
            Assert.IsNotNull(process);

            process.ReadFile(TestUtils.FullTestFilePath() + "TestDuplicate5.txt");
            process.FindDuplicates();

            TestDup5NormalResult(tester);
        }

        /// <summary>
        /// test the file TestDuplicate5.txt with exclusions
        /// </summary>
        [Test]
        public void TestDuplicate5WithValidExclusion()
        {
            FileDuplicateFinderTester tester = new FileDuplicateFinderTester();
            FileDuplicateFinder process = tester.Process;
            Assert.IsNotNull(process);

            process.AddIgnoredLinePrefix("duplicate");
            process.ReadFile(TestUtils.FullTestFilePath() + "TestDuplicate5.txt");
            process.FindDuplicates();

            // should have found no duplicates
            Assert.AreEqual(0, tester.DuplicateCount);
        }

        /// <summary>
        /// Test the file TestDuplicate5.txt with dummy exclusions
        /// </summary>
        [Test]
        public void TestDuplicate5WithDummyExclusions()
        {
            FileDuplicateFinderTester tester = new FileDuplicateFinderTester();
            FileDuplicateFinder process = tester.Process;
            Assert.IsNotNull(process);

            process.AddIgnoredLinePrefix("dummy");
            process.AddIgnoredLinePrefix("fish");
            process.AddIgnoredLinePrefix("spon");
            process.ReadFile(TestUtils.FullTestFilePath() + "TestDuplicate5.txt");
            process.FindDuplicates();

            TestDup5NormalResult(tester);
        }

        /// <summary>
        /// test the file Text1.txt
        /// </summary>
        [Test]
        public void TestProcessFile1()
        {
            FileDuplicateFinderTester tester = new FileDuplicateFinderTester();
            FileDuplicateFinder process = tester.Process;
            Assert.IsNotNull(process);

            process.ReadFile(TestUtils.FullTestFilePath() + "Text1.txt");
            process.FindDuplicates();

            Assert.AreEqual(0, tester.DuplicateCount);
        }

        /// <summary>
        /// Test the file Text2.txt
        /// </summary>
        [Test]
        public void TestProcessFile2()
        {
            FileDuplicateFinderTester tester = new FileDuplicateFinderTester();
            FileDuplicateFinder process = tester.Process;
            Assert.IsNotNull(process);
            
            process.ReadFile(TestUtils.FullTestFilePath() + "Text2.txt");
            process.FindDuplicates();

            Assert.AreEqual(0, tester.DuplicateCount);
        }

        /// <summary>
        /// Test the file GrowingTextBlocks5.txt
        /// </summary>
        [Test]
        public void TestProcessGrowingTextBlocks5Threshhold4()
        {
            FileDuplicateFinderTester tester = new FileDuplicateFinderTester();
            FileDuplicateFinder process = tester.Process;
            Assert.IsNotNull(process);

            process.ReadFile(TestUtils.FullTestFilePath() + "GrowingTextBlocks5.txt");
            process.DuplicateThreshold = 4;
            process.FindDuplicates();

            // one duplicate blocks (of four lines or more)
            Assert.AreEqual(1, tester.DuplicateCount);
            Assert.AreEqual(2, tester.LastDuplicateInstanceCount);
        }

        /// <summary>
        /// test the file GrowingTextBlocks5.txt with a threshhold of 5
        /// </summary>
        [Test]
        public void TestProcessGrowingTextBlocks5_Threshhold5()
        {
            FileDuplicateFinderTester tester = new FileDuplicateFinderTester();
            FileDuplicateFinder process = tester.Process;
            Assert.IsNotNull(process);

            process.ReadFile(TestUtils.FullTestFilePath() + "GrowingTextBlocks5.txt");
            process.DuplicateThreshold = 5;
            process.FindDuplicates();

            // no duplicate blocks (of five lines or more)
            Assert.AreEqual(0, tester.DuplicateCount);
        }

        /// <summary>
        /// Test the file GrowingTextBlocks12.txt with a threshhold of 4
        /// </summary>
        [Test]
        public void TestProcessGrowingTextBlocks12Threshold4()
        {
            FileDuplicateFinderTester tester = new FileDuplicateFinderTester();
            FileDuplicateFinder process = tester.Process;
            Assert.IsNotNull(process);

            process.ReadFile(TestUtils.FullTestFilePath() + "GrowingTextBlocks12.txt");
            process.DuplicateThreshold = 4;
            process.FindDuplicates();

            // two duplicate blocks
            Assert.AreEqual(2, tester.DuplicateCount);
            Assert.AreEqual(5, tester.LastDuplicateInstanceCount);
        }

        /// <summary>
        /// Test the file GrowingTextBlocks12.txt with a threshhold of 5
        /// </summary>
        [Test]
        public void TestProcessGrowingTextBlocks12Threshold5()
        {
            FileDuplicateFinderTester tester = new FileDuplicateFinderTester();
            FileDuplicateFinder process = tester.Process;
            Assert.IsNotNull(process);

            process.ReadFile(TestUtils.FullTestFilePath() + "GrowingTextBlocks12.txt");
            process.FindDuplicates();

            // two duplicate blocks
            Assert.AreEqual(2, tester.DuplicateCount);
            Assert.AreEqual(3, tester.LastDuplicateInstanceCount);
        }

        /// <summary>
        /// Test the file TestLongAndShort.txt
        /// </summary>
        [Test]
        public void TestLongAndShort()
        {
            FileDuplicateFinderTester tester = new FileDuplicateFinderTester();
            FileDuplicateFinder process = tester.Process;
            Assert.IsNotNull(process);

            process.ReadFile(TestUtils.FullTestFilePath() + "TestLongAndShort.txt");
            process.FindDuplicates();

            // two duplicate blocks
            Assert.AreEqual(2, tester.DuplicateCount);
            Assert.AreEqual(7, tester.LastDuplicateLength);
            Assert.AreEqual(2, tester.LastDuplicateInstanceCount);
        }

        /// <summary>
        /// test the files Text1.txt and Text2.txt
        /// </summary>
        [Test]
        public void TestProcessTwoFiles()
        {
            FileDuplicateFinderTester tester = new FileDuplicateFinderTester();
            FileDuplicateFinder process = tester.Process;
            Assert.IsNotNull(process);

            process.ReadFile(TestUtils.FullTestFilePath() + "Text1.txt");
            process.ReadFile(TestUtils.FullTestFilePath() + "Text2.txt");
            process.FindDuplicates();

            Assert.AreEqual(1, tester.DuplicateCount);
            Assert.AreEqual(2, tester.LastDuplicateInstanceCount);
        }

        /// <summary>
        /// Test the file Test5Lines1.txt and Test5Lines2.txt
        /// </summary>
        [Test]
        public void TestProcessTwoFiveLineFiles()
        {
            FileDuplicateFinderTester tester = new FileDuplicateFinderTester();
            FileDuplicateFinder process = tester.Process;
            Assert.IsNotNull(process);

            process.ReadFile(TestUtils.FullTestFilePath() + "Test5Lines1.txt");
            process.ReadFile(TestUtils.FullTestFilePath() + "Test5Lines2.txt");
            process.FindDuplicates();

            Assert.AreEqual(1, tester.DuplicateCount);
            Assert.AreEqual(2, tester.LastDuplicateInstanceCount);
            Assert.AreEqual(5, tester.LastDuplicateLength);
            Assert.AreEqual(2, tester.LastDuplicateFirstLine);
        }
        #endregion

        #region helpers

        /// <summary>
        /// check that the hormal result occured - of one duplicate 5 lines long
        /// </summary>
        /// <param name="tester">The test harness to inspect</param>
        private static void TestDup5NormalResult(BaseDuplicateFinderTester tester)
        {
            // should have found one duplicate of 5 lines long
            Assert.AreEqual(1, tester.DuplicateCount);
            Assert.AreEqual(5, tester.LastDuplicateLength);
            Assert.AreEqual(2, tester.LastDuplicateInstanceCount);
        }

        #endregion
    }
}
